

#ifndef CAN_CFG_H_
#define CAN_CFG_H_
#include "Can_GeneralTypes.h"

#if !(((CAN_SW_MAJOR_VERSION == 5) && (CAN_SW_MINOR_VERSION == 0)) )
#error Can: Configuration file expected BSW module version to be 5.0.*
#endif

#if !(((CAN_AR_MAJOR_VERSION == 4) && (CAN_AR_MINOR_VERSION == 1)) )
#error Can: Configuration file expected AUTOSAAR version to be 4.1.*
#endif

// Number of controller configs
#define CAN_ARC_CTRL_CONFIG_CNT			1

#define CAN_DEV_ERROR_DETECT			STD_ON
#define CAN_VERSION_INFO_API			STD_OFF
#define CAN_MULTIPLEXED_TRANSMISSION	STD_OFF  // Not supported
#define CAN_WAKEUP_SUPPORT				STD_OFF  // Not supported
#define CAN_HW_TRANSMIT_CANCELLATION	STD_OFF
#define ARC_CAN_ERROR_POLLING			STD_OFF
#define CAN_USE_WRITE_POLLING			STD_OFF
#define CAN_USE_READ_POLLING			STD_OFF
#define CAN_USE_BUSOFF_POLLING			STD_OFF
#define CAN_USE_WAKEUP_POLLING			STD_OFF  // Not supported

#define CAN_ARC_LOG_UNIT_CNT			1U
#define CAN_ARC_HW_UNIT_HIGH			0U
#define CAN_ARC_HW_UNIT_CNT				(CAN_ARC_HW_UNIT_HIGH + 1U) 

#define USE_RSCAN_CH0 STD_ON
#define USE_RSCAN_CH1 STD_OFF
#define USE_RSCAN_CH2 STD_OFF
#define USE_RSCAN_CH3 STD_OFF
#define USE_RSCAN_CH4 STD_OFF
#define USE_RSCAN_CH5 STD_OFF
#define USE_RSCAN_CH6 STD_OFF
#define USE_RSCAN_CH7 STD_OFF

#define NUM_RSCAN_UNITS 2
#define RSCAN0_IN_USE 1
#define RSCAN1_IN_USE 0
#define NUM_RSCAN_IN_USE  (RSCAN0_IN_USE + RSCAN1_IN_USE)

/* _CTRL_ is actually is actually the channel on this HW */
#define CAN_ARC_CTRL_CNT			3 /* IMPROVEMENT */
//#define CAN_ARC_CTRL_CONFIG_CNT		3 /* IMPROVEMENT */
#define CAN_ARC_CTRL_MAX			3 /* IMPROVEMENT */

#define NO_CANIF_CONTROLLER 0xFFU

#define RSCAN_CH0   0U
#define RSCAN_CH1   1U
#define	RSCAN_CH2   2U
#define	RSCAN_CH3   3U
#define	RSCAN_CH4   4U
#define	RSCAN_CH5   5U
#define	RSCAN_CH6   6U
#define	RSCAN_CH7   7U


typedef union {
     uint32_t R;
     struct {
         uint32 BEF:1; /* Bus Error Flag */
         uint32 EWF:1; /*  Error Warning Flag */
         uint32 EPF:1; /*  Error Passive Flag */
         uint32 BOEF:1; /*  Bus Off Entry Flag */
         uint32 BORF:1; /*  Bus Off Recovery Flag */
         uint32 OVLF:1; /*  Overload Flag */
         uint32 BLF:1; /*  Bus Lock Flag */
         uint32 ALF:1; /*  Arbitration-lost Flag */
         uint32 SERR:1; /*  Stuff Error Flag */
         uint32 FERR:1; /*  Form Error Flag */
         uint32 AERR:1; /*  ACK Error Flag */
         uint32 CERR:1; /*  CRC Error Flag */
         uint32 B1ERR:1; /*  Recessive Bit Error Flag */
         uint32 B0ERR:1; /*  Dominant Bit Error Flag */
         uint32 ADERR:1; /*  ACK Delimiter Error Flag */
         uint32 :1; /* Reserved */
         uint32 CRCREG:15; /* CRC Calculation Data */
         uint32 :1; /* Reserved */
     } B;
 } Can_Arc_ErrorType;
 
 typedef struct Can_HardwareObjectStruct {
      /* PC/PB, Specifies the type (Full-CAN or Basic-CAN) of a hardware object. */
      Can_Arc_HohType CanHandleType;

      /* PC/PB, Specifies whether the IdValue is of type - standard identifier - extended
       * identifier - mixed mode ImplementationType: Can_IdType */
      Can_IdTypeType CanIdType;

      /* PC/PB Specifies (together with the filter mask) the identifiers range that passes
       *  the hardware filter. */
      uint32 CanHwFilterCode;

      /* PC/PB Holds the handle ID of HRH or HTH. The value of this parameter is unique
       *  in a given CAN Driver, and it should start with 0 and continue without any
       *  gaps. The HRH and HTH Ids are defined under two different name-spaces.
       *  Example: HRH0-0, HRH1-1, HTH0-2, HTH1-3 */
      uint32 CanObjectId;

      /* PC/PB, Specifies if the HardwareObject is used as Transmit or as Receive object */
      Can_ObjectTypeType CanObjectType;

      /* PC/PB The filter mask that is used for hardware filtering together
       * with the CanHwFilterCode */
      Can_FilterMaskType CanHwFilterMask;

      /* PC,  See CAN_HOH_XX macros */
      uint32 Can_Arc_Flags;
} Can_HardwareObjectType;

typedef struct {

       boolean CanControllerActivation;

       //  This parameter provides the controller ID which is unique in a given CAN
       //  Driver. The value for this parameter starts with 0 and continue without any
       //  gaps.

       uint8 Can_Arc_HwUnitId;

       // Specifies the default baud rate in kbps (from the list CanControllerSupportedBaudrates)

       const Can_ControllerBaudrateConfigType *CanControllerDefaultBaudrate;

       // Specifies the supported baud rates
       const Can_ControllerBaudrateConfigType * const CanControllerSupportedBaudrates;

       // Specifices the length of the CanControllerSupportedBaudrates list
       uint16 CanControllerSupportedBaudratesCount;

       //  Reference to the CPU clock configuration, which is set in the MCU driver
       //  configuration
       uint32 CanCpuClockRef;

       //  This parameter contains a reference to the Wakeup Source for this
       //  controller as defined in the ECU State Manager. Implementation Type:
       //  reference to EcuM_WakeupSourceType
       uint32 CanWakeupSourceRef;

       // List of Hoh id's that belong to this controller
       const Can_HardwareObjectType  * const Can_Arc_Hoh;
  #if defined(CFG_CAN_USE_SYMBOLIC_CANIF_CONTROLLER_ID)
       uint8 Can_Arc_CanIfControllerId;
  #endif

       /* Flags, See CAN_CTRL_XX macros */
       uint32 Can_Arc_Flags;

       /* Total number of HOHs in Can_Arc_Hoh */
       uint32 Can_Arc_HohCnt;

       PduIdType * const Can_Arc_TxPduHandles;

       uint32 Can_Arc_RxFifoSz;
} Can_ControllerConfigType;

typedef struct {
     const Can_ControllerConfigType *CanController;

     const  uint8 * const ArcHthToSymbolicController;
     const  uint8 * const ArcHthToHoh;
     const  uint8 * const ArcHwUnitToController;
     const Can_HwHandleType * const ArcSymbolicHohToInternalHoh;
} Can_ConfigSetType;

 /** @req 4.1.2|4.3.0/SWS_Can_00413 Can_ConfigType*//* Can_ConfigType */
 typedef struct {
      // This is the multiple configuration set container for CAN Driver
      // Multiplicity 1..*
      const Can_ConfigSetType  *CanConfigSetPtr;
  } Can_ConfigType;
  
/* Type for holding global information used by the driver */
typedef struct {
    Can_DriverStateType initRun; /* If Can_Init() have been run */
    const Can_ConfigType * config; /* Pointer to config */

    uint8 channelMap[CAN_ARC_HW_UNIT_CNT];
    uint8 channelToHwChannel[CAN_ARC_LOG_UNIT_CNT];
} Can_GlobalType;
extern Can_GlobalType Can_Global;
/* Publish the configuration */
extern const Can_ConfigType CanConfigData;


#define Can_Arc_Error(_id,_err)


#define CanConf_CanController_CanController0	(uint8)0
#define Can_CanController0	CanConf_CanController_CanController0

#define CAN_ARC_CANHARDWAREOBJECT_CTRL_0_TX_0 (Can_HwHandleType)0
#define NUM_OF_HTHS (Can_HwHandleType)1

#define CAN_ARC_CANHARDWAREOBJECT_CTRL_0_RX_0 (Can_HwHandleType)1

#define CanConf_CanHardwareObject_Ctrl_0_Rx_0 (Can_HwHandleType)0
#define Can_Ctrl_0_Rx_0 CanConf_CanHardwareObject_Ctrl_0_Rx_0
#define CanConf_CanHardwareObject_Ctrl_0_Tx_0 (Can_HwHandleType)1
#define Can_Ctrl_0_Tx_0 CanConf_CanHardwareObject_Ctrl_0_Tx_0

#define NUM_OF_HOHS 2

/* Configuration Set Handles */
#define CanConfigSet (CanConfigData)
#define Can_CanConfigSet (CanConfigData)


/* CanControllerBaudRateConfigID */
#define CanConf_CanControllerBaudrateConfig_CanControllerBaudrateConfig 0


/* HOH flags */
#define CAN_HOH_FIFO_MASK           (1UL)

/* Controller flags */
#define CAN_CTRL_RX_PROCESSING_INTERRUPT        ((uint32)1u)
#define CAN_CTRL_RX_PROCESSING_POLLING          0u
#define CAN_CTRL_TX_PROCESSING_INTERRUPT        ((uint32)1u<<1u)
#define CAN_CTRL_TX_PROCESSING_POLLING          0u
#define CAN_CTRL_WAKEUP_PROCESSING_INTERRUPT    ((uint32)1u<<2u)
#define CAN_CTRL_WAKEUP_PROCESSING_POLLING      0u
#define CAN_CTRL_BUSOFF_PROCESSING_INTERRUPT    ((uint32)1u<<3u)
#define CAN_CTRL_BUSOFF_PROCESSING_POLLING      0u
#define CAN_CTRL_ACTIVATION                     ((uint32)1u<<4u)

#define CAN_CTRL_LOOPBACK                       ((uint32)1u<<5u)
#define CAN_CTRL_FIFO                           ((uint32)1u<<6u)

#define CAN_CTRL_ERROR_PROCESSING_INTERRUPT     ((uint32)1u<<7u)
#define CAN_CTRL_ERROR_PROCESSING_POLLING       0u




#endif /*CAN_CFG_H_*/
